package com.example.springboot.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.common.Result;
import com.example.springboot.entity.User;
import com.example.springboot.entity.Users;
import com.example.springboot.mapper.UsersMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

// 管理员
@RestController
@RequestMapping("/users")
public class UsersController {
    /**
     * @RequestBody将传来的对象转化为JSON
     */
    @Resource
    private UsersMapper usersMapper;

    /**
     * 登录接口
     */
    @PostMapping("login")
    public Result<?> login(@RequestBody Users users) {
        Users res = usersMapper.selectOne(Wrappers.<Users>lambdaQuery().eq(Users::getUsername, users.getUsername()).eq(Users::getPassword, users.getPassword()));
        if (res == null) {
            return Result.error("-1", "用户名或密码错误");
        }
        return Result.success(res);
    }

    /**
     * 注册接口
     */
    @PostMapping("register")
    public Result<?> register(@RequestBody Users users) {
        Users res = usersMapper.selectOne(Wrappers.<Users>lambdaQuery().eq(Users::getUsername, users.getUsername()));
        if (res != null) {
            return Result.error("-1", "用户名重复");
        }
        if (users.getPassword() == null) {
            users.setPassword("123456");
        }
        usersMapper.insert(users);
        return Result.success();
    }


    /**
     * 新增
     */
    @PostMapping
    public Result<?> save(@RequestBody Users users) {
        if (users.getPassword() == null) {
            users.setPassword("123456");
        }
        usersMapper.insert(users);
        return Result.success();
    }

    /**
     * 编辑
     */
    @PutMapping
    public Result<?> update(@RequestBody Users users) {
        usersMapper.updateById(users);
        return Result.success();
    }

    /**
     * 根据id查数据
     */
    @GetMapping("/{id}")
    public Result<?> getById(@PathVariable Long id) {
        usersMapper.selectById(id);
        return Result.success();
    }

    /**
     * 删除
     * "/{id}"这种占位符数据获取需要@PathVariable注解来实现
     * "/{id}/{aa}"  @PathVariable Long id ,@PathVariable String aa 就可以取到两个值
     */
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        usersMapper.deleteById(id);
        return Result.success();
    }


    /**
     * 分页模糊查询
     */
    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "5") Integer pageSize,
//                              @RequestParam(defaultValue = "" ) Integer counts,
                              @RequestParam(defaultValue = "") String search) {
        LambdaQueryWrapper<Users> wrapper = Wrappers.<Users>lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(Users::getUsername, search);
        }
        Page<Users> usersPage = usersMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(usersPage);
    }
}
